﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OTS.BLL;

public partial class OLPC_MCActionRecord : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
                SettingRight();
                Session["importeddtlist"] = null;
                Session["assigneddt"] = null;
                InitUi();
            }
            catch (Exception ex)
            {
                Log.WritLog("Page_Load:", ex.ToString());
            }
        }
    }
    // 页面权限
    private void SettingRight()
    {
        bool[] rightlist = SysRightBLL.GetRightByType(new string[] { 
            Session["UserCode"].ToString(), 
            Session["CoCode"].ToString(), 
            "mcactionrecord"});
        if (!rightlist[0])
        { this.Page.Response.Write(String.Format("<script language='javascript'>window.parent.location.href='{0}';</script>", CommonModel.loginOut1)); }

        gvimported.Columns[0].Visible = rightlist[3];
        gvAssigned.Columns[0].Visible = rightlist[3];
    }
    // 初始化绑定
    private void InitUi()
    {
        CommonMethod.IsTimeOutUserCode(this);
        InitImportDataBind();
        InitAssignedDataBind();
    }
    // 已有数据
    private void InitImportDataBind()
    {
        DataTable importeddt = new DataTable();
        if (Session["importeddtlist"] != null)
        {
            importeddt = Session["importeddtlist"] as DataTable;
        }
        else
        {
            importeddt = MCActionRecordBLL.ImportedRecordGetList("");
        }
        gvimported.DataSource = importeddt;
        gvimported.DataBind();
        Session["importeddtlist"] = importeddt;
    }
    protected void gvimported_AfterPerformCallback(object sender,
        DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InitImportDataBind();
    }
    // 删除、修改
    protected void gvimported_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        try
        {
            string temp = e.CommandArgs.CommandArgument.ToString();
            string cmd = e.CommandArgs.CommandName;

            if (cmd == "delete")
            {
                bool flag = MCActionRecordBLL.ImportedRecordDelete(temp);
                if (flag)
                {
                    Session["importeddtlist"] = null;
                    InitImportDataBind();
                }
                else
                    CommonMethod.regAlertMessage(udp, "删除失败！", this);
            }
        }
        catch (Exception ex)
        {
            Log.WritLog("gvimported_RowCommand:", ex.ToString());
        }
    }


    // 已匹配收款数据
    private void InitAssignedDataBind()
    {
        DataTable tempdt = new DataTable();
        if (Session["assigneddt"] != null)
        {
            tempdt = Session["assigneddt"] as DataTable;
        }
        else
        {
            tempdt = MCActionRecordBLL.AppliedRecordGetList("");
            gvAssigned.DataSource = tempdt;
            gvAssigned.DataBind();
        }
        gvAssigned.DataSource = tempdt;
        gvAssigned.DataBind();
        Session["assigneddt"] = tempdt;
    }

    protected void gvAssigned_AfterPerformCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewAfterPerformCallbackEventArgs e)
    {
        InitAssignedDataBind();
    }
    protected void gvAssigned_RowCommand(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewRowCommandEventArgs e)
    {
        try
        {
            string temp = e.CommandArgs.CommandArgument.ToString();
            string cmd = e.CommandArgs.CommandName;
            if (cmd == "delete")
            {
                bool resultFlag = MCActionRecordBLL.AppliedRecordDelete(temp);
                if (resultFlag)
                {
                    Session["assigneddt"] = null;
                    InitAssignedDataBind();
                }
                else
                    CommonMethod.regAlertMessage(udp, "删除失败！", this);
            }
        }
        catch (Exception ex)
        {
            Log.WritLog("gvAssigned_RowCommand:", ex.ToString());
        }
    }

    public Boolean GetVisible(string flag)
    {
        return "1".Equals(flag);
    }

    public bool CreditChecked(string CreditCheck)
    {
        return "1" == CreditCheck;
    }
}